package com.janlei.tdd.lintcode.t_2424;

import java.util.Optional;

/**
 * 打印杨辉三角
 */
public class T_2424 {

    int[][] print(int row) {

        Integer param = Optional.of(row)
                .filter(num -> num > 0)
                .orElseThrow(() -> new IllegalArgumentException("Illegal Argument"));

        return result(param);
    }

    private int[][] result(int row) {
        int[][] res = new int[row][row];

        // x row   y col
        // x=y   f(x,y)=1
        // x=0,y=0  f(x,y)=1
        // f(x,y)=f(x-1,y-1)+f(x,y-1)


        for (int i = 0; i < row; i++) {
            for (int j = 0; j <= i; j++) {
                boolean isBorder = i == j || i == 0 || j == 0;
                if (isBorder) {
                    res[i][j] = 1;
                } else {
                    res[i][j] = res[i - 1][j - 1] + res[i - 1][j];
                }
            }
        }


        return res;
    }
}
